﻿using ETask.Api.Data;
using ETask.Api.Dtos;
using ETask.Api.Models;
using Microsoft.EntityFrameworkCore;

namespace ETask.Api.Repositories
{
    public class ProjectDocRepository : Repository<ProjectDoc>, IProjectDocRepository
    {
        public ProjectDocRepository(AppDbContext context) : base(context)
        {
        }

        public async Task<ProjectDocDto> GetInfoByIdAsync(int id)
        {
            var document = await _context.ProjectDocs
                .Join(_context.Users,
                    doc => doc.CreateBy,
                    user => user.Id,
                    (doc, user) => new { doc, user })
                .FirstOrDefaultAsync(q => q.doc.Id == id);
            if (document == null)
            {
                return null;
            }
            return new ProjectDocDto
            {
                Id = document.doc.Id,
                Title = document.doc.Title,
                ProjectId = document.doc.ProjectId,
                Content = document.doc.Content,
                FolderId = document.doc.FolderId,
                IsShare = document.doc.IsShare,
                Tips = document.doc.Tips,
                CreatedAt = document.doc.CreatedAt,
                CreateByName = document.user.Name
            };
        }

        public async Task<PagedResult<ProjectDocDto>> GetListAsync(ProjectDocListParams listParams)
        {
            var query = _context.ProjectDocs.Join(_context.Users, doc => doc.CreateBy, user => user.Id, (doc, user) => new { doc, user })
                .Where(q => q.doc.ProjectId == listParams.ProjectId);
            if (!string.IsNullOrWhiteSpace(listParams.KeyWord))
            {
                query = query.Where(q => q.doc.Title.Contains(listParams.KeyWord));
            }
            if (!string.IsNullOrWhiteSpace(listParams.Tip))
            {
                query = query.Where(q => q.doc.Tips.Contains(listParams.Tip));
            }
            if (listParams.IsShare.HasValue)
            {
                query = query.Where(q => q.doc.IsShare == listParams.IsShare.Value);
            }
            query = query.OrderByDescending(q => q.doc.CreatedAt);
            return await query
                .Select(q => new ProjectDocDto
                {
                    Id = q.doc.Id,
                    Title = q.doc.Title,
                    ProjectId = q.doc.ProjectId,
                    Content = q.doc.Content,
                    FolderId = q.doc.FolderId,
                    IsShare = q.doc.IsShare,
                    Tips = q.doc.Tips,
                    CreatedAt = q.doc.CreatedAt,
                    CreateByName = q.user.Name
                }).ToPagedResultAsync(listParams.PageDto);
        }

        public async Task<List<ItemDto>> GetTipsAsync(int projectId)
        {
            var list = await _context.ProjectDocs.Where(q => q.ProjectId.Equals(projectId))
                .GroupBy(q => q.Tips)
                .ToListAsync();
            return list.Select(q => new ItemDto
            {
                Key = q.Key,
                Count = q.Count()
            }).ToList();
        }
    }
}
